package com.cpi.cornpp.instructionspurchases.service;

import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructions.futures.services.InstructionsFuturesService;
import com.cpi.cornpp.instructionsforecasts.service.InstructionForecastsService;
import com.cpi.cornpp.instructionspurchaseforecast.dto.InstructionPurchaseForecastSearchDTO;
import com.cpi.cornpp.instructionspurchaseforecast.view.InstructionPurchaseForecastSearchView;
import com.cpi.cornpp.instructionspurchases.converter.InstructionPurchaseConverter;
import com.cpi.cornpp.instructionspurchases.dao.InstructionPurchaseDAO;
import com.cpi.cornpp.instructionspurchases.dto.InstructionPurchaseDTO;
import com.cpi.cornpp.instructionspurchases.dto.InstructionPurchaseSearchDTO;
import com.cpi.cornpp.instructionspurchases.exception.InstructionPurchaseException;
import com.cpi.cornpp.instructionspurchases.view.InstructionPurchaseCreateView;
import com.cpi.cornpp.instructionspurchases.view.InstructionPurchaseSearchView;
import com.cpi.cornpp.product.view.ProductView;
import com.cpi.cornpp.purchaseinstructiondetail.converter.InstructionPurchaseDetailConverter;
import com.cpi.cornpp.purchaseinstructiondetail.dao.InstructionPurchaseDetailDAO;
import com.cpi.cornpp.purchaseinstructiondetail.dto.InstructionPurchaseDetailDTO;
import com.cpi.cornpp.purchaseinstructiondetail.view.InstructionPurchaseDetailView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class InstructionPurchaseService {
	
	private static final String INSTRUCTION_PURCHASE_STATUS_CANCELED_ID = "INSTRUCTION_PURCHASE_STATUS_CANCELED_ID";


	public List getCreateList(InstructionPurchaseSearchView instructionPurchaseSearchView,int orderField, boolean ascendingOrder)throws InstructionPurchaseException{
		List instructionPurchaseViews = null;
		List instructionPurchaseDTOs = null;
		InstructionPurchaseDAO instructionPurchaseDAO = null;
		InstructionPurchaseSearchDTO instructionPurchaseSearchDTO;
		InstructionPurchaseConverter instructionPurchaseConverter = null;
		try{
			instructionPurchaseDAO = new InstructionPurchaseDAO();
			instructionPurchaseConverter = new InstructionPurchaseConverter();
			instructionPurchaseSearchDTO = instructionPurchaseConverter.convertViewToSearchDTO(instructionPurchaseSearchView);
			instructionPurchaseDTOs = instructionPurchaseDAO.list(instructionPurchaseSearchDTO,orderField, ascendingOrder);
			instructionPurchaseViews = instructionPurchaseConverter.convertDTOsToCreateViews(instructionPurchaseDTOs);
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseViews;
	}
	/**
	 * Metodo que obtiene una
	 * Instruccion de Compra
	 *
	 * @author Edgar Joao
	 * @param id - purchaseInstructionId
	 * @return
	 * @throws InstructionPurchaseException
	 */
	public InstructionPurchaseCreateView getInstructionPurchase(int id) throws InstructionPurchaseException{
		InstructionPurchaseCreateView view = null;		
		InstructionPurchaseDAO dao = null;
		InstructionPurchaseConverter converter = null;
		try{
			dao = new InstructionPurchaseDAO();
			converter = new InstructionPurchaseConverter();
			view = converter.convertDTOToView(dao.getInstructionPurchaseDTO(id));									
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return view;
	}	
	/**
	 * Metodo que agrega una nueva Instrucci�n de Compra
	 * <h1> Header de la tabla Principal </h1>
	 * 
	 * @author Edgar Joao
	 * @param view
	 * @throws InstructionPurchaseException
	 */
	public void insertOrUpdatePurchaseInstruction(InstructionPurchaseCreateView view, List instructionPurchaseFormDetails)throws InstructionPurchaseException{
		InstructionPurchaseDTO dto = null;
		InstructionPurchaseDAO instructionPurchaseDAO = null;
		InstructionPurchaseDetailDAO instructionPurchaseDetailDAO = null;
		
		InstructionForecastsService forecastService = null;
		InstructionsFuturesService futureService = null;
		
		InstructionPurchaseConverter converter = null;
		InstructionPurchaseDetailConverter instructionPurchaseDetailConverter = null;
		Iterator iterator = null;
		InstructionPurchaseDetailView instructionPurchaseDetailView = null;
		InstructionPurchaseDetailDTO instructionPurchaseDetailDTO = null;
		int purchaseInstructionId = 0;
		int int_i = 0;
		SystemParameterCollection systemParameterCollection;
		SystemParameterBean systemParameterBean;
		
		forecastService = new InstructionForecastsService();
		futureService = new InstructionsFuturesService();
		Iterator productsIterator = null;
		ProductView productView = null;
		try{
			systemParameterCollection = SystemParameterCollection.getInstance();
			instructionPurchaseDAO = new InstructionPurchaseDAO();
			instructionPurchaseDetailDAO = new InstructionPurchaseDetailDAO();
			dto = new InstructionPurchaseDTO();
			converter = new InstructionPurchaseConverter();
			instructionPurchaseDetailConverter = new InstructionPurchaseDetailConverter();
			dto = converter.convertViewToDTO(view);
			if(view.getId() == 0){
				//INSERTA PURCHASE INSTRUCTION
				purchaseInstructionId = instructionPurchaseDAO.insertNewPurchaseInstruction(dto);
				view.setId(purchaseInstructionId);
				view.setAddAction(true);
				if( instructionPurchaseFormDetails != null ){
					iterator = instructionPurchaseFormDetails.iterator();
					while( iterator.hasNext() ){
						instructionPurchaseDetailView = (InstructionPurchaseDetailView)iterator.next();
						instructionPurchaseDetailView.setPurchaseInstructionId(purchaseInstructionId);
						instructionPurchaseDetailView.setRow(String.valueOf(++int_i));
						instructionPurchaseDetailDTO = instructionPurchaseDetailConverter.converterPurchaseInstructionDetailViewToDto(instructionPurchaseDetailView);
						//INSERTA PURCHASE INSTRUCTION DETAIL 
						instructionPurchaseDetailDAO.insert(instructionPurchaseDetailDTO);
						instructionPurchaseDetailView.setPurchaseInstructionDetailid(instructionPurchaseDetailDTO.getPurchaseInstructionDetailid());
						//INSERTAR PRODUCTOS PARA EL DETALLE
						productsIterator = instructionPurchaseDetailView.getProductList().iterator();
						while(productsIterator.hasNext()){
							productView = (ProductView)productsIterator.next();
							instructionPurchaseDetailDAO.insertItemDetail(instructionPurchaseDetailDTO.getPurchaseInstructionDetailid(), productView.getProductId());
						}
						//INSERTAR LOS FORECASTS DE CADA PURCHASE INSTRUCTION DETAIL
						forecastService.updateInstructionsForecastsViews(instructionPurchaseDetailView.getInstructionsForecastsDetailView().getDetails(), instructionPurchaseDetailView.getInstructionsForecastsDetailView().getChecked(),instructionPurchaseDetailView.getPurchaseInstructionDetailid());
						//INSERTAR LOS FUTUROS DE CADA PURCHASE INSTRUCTION DETAIL
						futureService.saveInstructionsFutures(instructionPurchaseDetailView.getInstructionsFuturesView(), instructionPurchaseDetailView.getPurchaseInstructionDetailid());						
						
					}
				}
			}else{
				systemParameterBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_STATUS_CANCELED_ID);
				instructionPurchaseDAO.updateNewPurchaseInstruction(dto);
				if( dto.getPurchaseStatus() == systemParameterBean.getIntValue() ){
					instructionPurchaseDetailDAO.changeStatusInstructionsDetail(dto.getId(), systemParameterBean.getIntValue());
				}
				view.setAddAction(false);
			}						
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}	
	}
	
	/**
	 * Metodo que agrega una nueva Instrucci�n de Compra
	 * <h1> Header de la tabla Principal </h1>
	 * 
	 * @author Edgar Joao
	 * @param view
	 * @throws InstructionPurchaseException
	 */
	public void insertOrUpdatePurchaseInstruction(InstructionPurchaseCreateView view, List instructionPurchaseFormDetails, boolean instructionPurchaseDetailHasChildren )throws InstructionPurchaseException{
		InstructionPurchaseDTO dto = null;
		InstructionPurchaseDAO instructionPurchaseDAO = null;
		InstructionPurchaseDetailDAO instructionPurchaseDetailDAO = null;
		InstructionPurchaseConverter converter = null;
		InstructionPurchaseDetailConverter instructionPurchaseDetailConverter = null;
		Iterator iterator = null;
		InstructionPurchaseDetailView instructionPurchaseDetailView = null;
		InstructionPurchaseDetailDTO instructionPurchaseDetailDTO = null;
		int purchaseInstructionId = 0;
		int int_i = 0;
		SystemParameterCollection systemParameterCollection;
		SystemParameterBean systemParameterBean;
		try{
			systemParameterCollection = SystemParameterCollection.getInstance();
			instructionPurchaseDAO = new InstructionPurchaseDAO();
			instructionPurchaseDetailDAO = new InstructionPurchaseDetailDAO();
			dto = new InstructionPurchaseDTO();
			converter = new InstructionPurchaseConverter();
			instructionPurchaseDetailConverter = new InstructionPurchaseDetailConverter();
			dto = converter.convertViewToDTO(view);
			if(view.getId() == 0){
				purchaseInstructionId = instructionPurchaseDAO.insertNewPurchaseInstruction(dto);
				if( instructionPurchaseFormDetails != null ){
					iterator = instructionPurchaseFormDetails.iterator();
					while( iterator.hasNext() ){
						instructionPurchaseDetailView = (InstructionPurchaseDetailView)iterator.next();
						instructionPurchaseDetailView.setPurchaseInstructionId(purchaseInstructionId);
						instructionPurchaseDetailView.setRow(String.valueOf(++int_i));
						instructionPurchaseDetailDTO = instructionPurchaseDetailConverter.converterPurchaseInstructionDetailViewToDto(instructionPurchaseDetailView);
						instructionPurchaseDetailDAO.insert(instructionPurchaseDetailDTO);
					}
				}
			}else{
				systemParameterBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_STATUS_CANCELED_ID);
				instructionPurchaseDAO.updateNewPurchaseInstruction(dto);
				if( dto.getPurchaseStatus() == systemParameterBean.getIntValue() ){
					instructionPurchaseDetailDAO.changeStatusInstructionsDetail(dto.getId(), systemParameterBean.getIntValue());
				}
			}						
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}	
	}
	
	
	/**
	 * 
	 * @param searchView
	 * @param orderField
	 * @param ascendingOrder
	 * @return
	 * @throws InstructionPurchaseException
	 */
	public List listInstructionPurchaseForecasts(InstructionPurchaseForecastSearchView searchView,int orderField, boolean ascendingOrder)throws InstructionPurchaseException{
												  
		List instructionPurchaseForecastViews = null;
		List instructionPurchaseForecastDTOs = null;
		InstructionPurchaseDAO instructionPurchaseDAO = null;
		InstructionPurchaseForecastSearchDTO instructionPurchaseForecastSearchDTO;
		InstructionPurchaseConverter instructionPurchaseConverter = null;
		try{
			instructionPurchaseDAO = new InstructionPurchaseDAO();
			instructionPurchaseConverter = new InstructionPurchaseConverter();
			instructionPurchaseForecastSearchDTO = instructionPurchaseConverter.convertViewToSearchDTO(searchView);
			instructionPurchaseForecastDTOs = instructionPurchaseDAO.listInstructionPurchaseForecasts(instructionPurchaseForecastSearchDTO,orderField, ascendingOrder);
			instructionPurchaseForecastViews = instructionPurchaseConverter.convertDTOsToForecastViews(instructionPurchaseForecastDTOs);
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseForecastViews;
		
	}
	/**
	 * Metodo que regresa una lista de
	 * Detalle Pronostico de Ventas
	 * 
	 * @author Edgar Joao
	 * @param periodId
	 * @param customerId
	 * @param productId
	 * @return
	 * @throws InstructionPurchaseException
	 */
	public List getInstructionForecastDetail(int periodId, String customerId, String productId) throws InstructionPurchaseException{
		List instructionPurchaseForecastViews = null;
		List instructionPurchaseForecastDTOs = null;
		InstructionPurchaseDAO instructionPurchaseDAO = null;					
		InstructionPurchaseConverter instructionPurchaseConverter = null;		
		try{
			instructionPurchaseDAO = new InstructionPurchaseDAO();
			instructionPurchaseConverter = new InstructionPurchaseConverter();			
			instructionPurchaseForecastDTOs = instructionPurchaseDAO.getInstructionForecastDetail(periodId, customerId, productId);							
			instructionPurchaseForecastViews = instructionPurchaseConverter.convertDTOsToForecastViews(instructionPurchaseForecastDTOs);					
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException= new InstructionPurchaseException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseForecastViews;
	}	
}